package com.sumainfor.pc.market.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.sumainfor.common.config.RedisUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.HousingMapper;
import com.sumainfor.pc.market.feign.BaseFeignEpc;
import com.sumainfor.pc.market.feign.BaseFeignZuul;
import com.sumainfor.pc.market.service.HousingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 功能描述:
 * 房源信息
 * @Auther: zhlu
 * @Date: 2018/12/15 15:07
 * @Description:
 */
@RestController
@RequestMapping("/housing")
public class HousingController {
    Logger log= LoggerFactory.getLogger(HousingController.class);

    @Autowired
    BaseFeignZuul BaseFeignZuul;

    @Autowired
    HousingService housingService;

    @Autowired
    BaseFeignEpc feignEpcController;

    @Autowired
    RedisUtils RedisUtils;

    @Autowired
    private HousingMapper housingMapper;

    /**
     * 功能描述:
     * 获取盈利预测中已通过审核的房源
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/15 15:06
     */
    @ResponseBody
    @RequestMapping(value = "/getProfitHousing",method = RequestMethod.GET)
    public JsonResult getProfitHousing(@RequestParam Map<String,Object> params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"ProId"})){
            result.putFailed("ProId不能为空！");
            return  result;
        }
        Map<String,Object>getProfitHousing=housingService.getProfitHousing(params);
//        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
//        log.info("getDeptList----------"+getDeptList);
//
//        if(FieldCheckUtil.isMapFieldNotNull(getProfitHousing,new String[]{"MiId"})){
//            getProfitHousing=ListMapMergeUtil.MapTranStringUtils(getProfitHousing,getDeptList,"MiId","DeptId","DeptName","MiName");
//        }
        if(FieldCheckUtil.isMapFieldNotNull(getProfitHousing,new String[]{"MiId"})){
            Map<String,Object>getDeptByDeptIdMap=BaseFeignZuul.getDeptByDeptIdMap(new HashMap<String, Object>(){{
                put("DeptId",getProfitHousing.get("MiId"));
            }});
            getProfitHousing.putAll(getDeptByDeptIdMap);
        }

        result.put(getProfitHousing);
        return result;
    }

    /**
     * 功能描述:
     * 新增收房信息
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/18 14:30
     */
    @ResponseBody
    @RequestMapping(value = "/AddHousing",method = RequestMethod.POST)
    public JsonResult AddHousing(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        Map<String,Object>AddHousing=housingService.AddHousing(params);
        if(AddHousing.get("status").equals(0)){//说明新增成功
            AddHousing.put("errorMessage","新增成功！");
            result.put(AddHousing);
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 房源列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/18 18:05
     */
    @ResponseBody
    @GetMapping("/getHousingList")
    public JsonResult getHousingList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        params.put("delfg",0);
        Integer state=Integer.valueOf(params.get("State").toString());
        List<String> State = new ArrayList<String>();
        if(state==1){
            State.add("1");
        }else if(state==2){
            State.add("2");
            State.add("3");
            State.add("4");
            State.add("5");
            State.add("6");
            State.add("7");
        }
//        List<String> State = (List<String>) params.get("State");
        params.put("State",State);
        log.info("State----"+State);
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=housingService.getHousingListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>> getHousingList=housingService.getHousingList(params);
        if(getHousingList.size()>0){
            List<Map<String,Object>>getPostDeptList=BaseFeignZuul.getPostDeptList(new HashMap<>());
            //合并
            getHousingList= ListMapMergeUtil.ListMergeUtils(getHousingList,getPostDeptList,"DeptId");
        }
//        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
//        for(Map<String,Object> map:getHousingList){
//            if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"MiId"})){
//                map=ListMapMergeUtil.MapTranStringUtils(map,getDeptList,"MiId","DeptId","DeptName","MiName");
//            }
//        }

        result=pageUtils.getJsonResult(getHousingList, params, cout);
        return result;
    }

    /**
     * 功能描述:
     * 根据房源编号获取房源资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/19 10:13
     */
    @ResponseBody
    @RequestMapping(value = "/getHousingMap",method = RequestMethod.GET)
    public JsonResult getHousingMap(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        params.put("delfg",0);
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"HouId"})){
            result.putFailed("HouId不能为空！");
            return  result;
        }
        //服务中心
        Map<String,Object>getHousingMap=housingService.getHousingMap(params);
        params.put("UserId",getHousingMap.get("SysUserId"));
        List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
        log.info("getDeptList-----------------"+getDeptList);
        if(FieldCheckUtil.isMapFieldNotNull(getHousingMap,new String[]{"MiId"})){
            getHousingMap=ListMapMergeUtil.MapTranStringUtils(getHousingMap,getDeptList,"MiId","DeptId","DeptName","MiName");
        }
        // 管家
        JsonResult getUserMap=BaseFeignZuul.getUserMap(new HashMap<String,Object>(){{
            put("UserId",params.get("UserId"));
        }});
        if(getUserMap.get("errorCode").equals("0")){
            getHousingMap.putAll((Map<String,Object>)getUserMap.getResult());
        }

        //查询工工程部
        JsonResult HousingPlan=new JsonResult();
        HousingPlan=feignEpcController.getHousingPlanList(params);
        if(HousingPlan.get("errorCode").equals("0")){
            getHousingMap.put("HousingPlanList",HousingPlan.get("result"));
            result.put(getHousingMap);
        }else{
            result.putFailed("工程部服务器繁忙，请稍后重试！");
        }



        return result;
    }
    
    /**
     * 功能描述: 
     * 修改房源信息
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/19 15:13
     */
    @ResponseBody
    @RequestMapping(value = "/updateHousing",method = RequestMethod.POST)
    public JsonResult updateHousing(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(housingService.updateHousing(params)){
            result.putSuccess("保存成功！");
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述:
     * 房源选择完成配置
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/19 15:13
     */
    @ResponseBody
    @RequestMapping(value = "/AddHousingInfo",method = RequestMethod.POST)
    public JsonResult AddHousingInfo(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        JsonResult feign=new JsonResult();
        feign=feignEpcController.AddHousingPlan(params);
        if(feign.get("errorCode").equals("0")){
            if(housingService.AddHousingInfo(params)){
                result.putSuccess("房源收录成功！");
            }else{
                result.putFailed("服务器繁忙，请稍后重试！");
            }
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        log.info("feign----"+feign);
        return result;
    }

    /**
     * 功能描述:
     * 从Redis去房源编号查询配置变更的房源
     * @Author zhlu
     * @Description //TODO
     * @Date 2018/12/24 15:33
     */
    @GetMapping("/getHousingPlanAmendList")
    public JsonResult getHousingPlanAmendList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        JsonResult feign=new JsonResult();
        params.put("delfg",0);
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        feign=feignEpcController.getHousingPlanAmendList();
        if(feign.get("errorCode").equals("0")){
            String json = JSONObject.toJSONString(feign.get("result"), SerializerFeature.WriteNullStringAsEmpty);
            List<Map<String,Object>>getHousingPlanAmendList=JSONObject.parseObject(json, List.class);
            if(getHousingPlanAmendList.size()>0){
                List ids=new ArrayList();
                for(Map<String,Object> map:getHousingPlanAmendList){
                    ids.add(map.get("HouId"));
                }
                params.put("HouIdList",ids);
                Integer cout=housingService.getHousingPlanAmendListCoun(params);
                if(cout==0){
                    return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
                }
                List<Map<String,Object>> getEpcHousingPlanHousingList=housingService.getHousingPlanAmendList(params);
//                List<Map<String,Object>>getDeptList=BaseFeignZuul.getDeptList(params);
//                getEpcHousingPlanHousingList= ListMapMergeUtil.ListTranStringUtils(getEpcHousingPlanHousingList,getDeptList,"MiId","DeptId","DeptName","MiName");
//                for(Map<String,Object> map:getEpcHousingPlanHousingList){
//                    if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"MiId"})){
//                        map=ListMapMergeUtil.MapTranStringUtils(map,getDeptList,"MiId","DeptId","DeptName","MiName");
//                    }
//                }
                if(getEpcHousingPlanHousingList.size()>0){
                    List<Map<String,Object>>getPostDeptList=BaseFeignZuul.getPostDeptList(new HashMap<>());
                    //合并
                    getEpcHousingPlanHousingList= ListMapMergeUtil.ListMergeUtils(getEpcHousingPlanHousingList,getPostDeptList,"DeptId");
                }

                result=pageUtils.getJsonResult(getEpcHousingPlanHousingList, params, cout);
            }else{//如果工程部返回的集合大小小于0或者等于0，就直接返回空列表
                return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
            }
            log.info("getHousingPlanAmendList----"+getHousingPlanAmendList);
        }else{
            result.putFailed("服务器繁忙，请稍后重试！");
        }
        return result;
    }

    /**
     * 功能描述: 
     * 获取房源验收之后定价
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/2/12 20:15
     */
    @GetMapping("/getPricingHousingList")
    public JsonResult getPricingHousingList(@RequestParam Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        params.put("IfOperaType",1);
        params.put("delfg",0);
        params.put("State",7);
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=housingService.getPricingHousingListCont(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>>getPricingHousingList=housingService.getPricingHousingList(params);
        result=pageUtils.getJsonResult(getPricingHousingList, params, cout);
        return result;
    }


    /**
     * 功能描述: 
     * 根据房源编号获取定价房源详情
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/2/13 18:39
     */
    @GetMapping("/getPricingHousingInfoList")
    public JsonResult getPricingHousingInfoList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"HouId"})){
            result.putFailed("HouId不能为空！");
            return  result;
        }
        result.put(housingService.getPricingHousingInfoList(params));
        return result;
    }

    /**
     * 功能描述: 
     * 获取所有房源房间
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/2/14 19:38
     */
    @ResponseBody
    @RequestMapping(value = "/getHousingActList",method = RequestMethod.POST)
    public JsonResult getHousingActList(@RequestBody Map<String,Object>params,Pager pager){
        JsonResult result=new JsonResult();
        params.put("delfg",0);
        //当前活动已选房源
        List<String> HouInfoIdList = new ArrayList<String>();
        List<Map<String, Object>> exclude = (List<Map<String, Object>>) params.get("exclude");
        if(exclude.size()>0){
            for (Map<String, Object> map:exclude){
                log.info("map----------"+map);
                HouInfoIdList.add(map.get("HouInfoId").toString());
            }
            params.put("HouInfoIdList",HouInfoIdList);
        }
        //状态查询
        List<String> State = (List<String>) params.get("State");
        List<String> StateList=new ArrayList<>();
        if(State.size()>0){
            for(String str:State){
                StateList.add(str);
            }
            params.put("StateList",StateList);
        }
        log.info("HouInfoIdList------"+HouInfoIdList);
        List<Map<String,Object>>allPricingHousingList=housingService.getHousingActList(params);
        pager.setPageno((Integer)params.get("pageno"));
        pager.setPagesize((Integer)params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=housingService.getHousingActListCount(params);
        if(cout==0){
            return pageUtils.getAllJsonResult(new ArrayList<Map<String,Object>>(),new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>>getPricingHousingList=housingService.getHousingActList(params);
        result=pageUtils.getAllJsonResult(getPricingHousingList,allPricingHousingList, params, cout);
        return result;
    }

    /**
     * 功能描述: 
     * 装修计划判断当前已选中房源的服务中心
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/3 11:01
     */
    @GetMapping("/getHousingMiMap")
    public JsonResult getHousingMiMap(){
        JsonResult result=new JsonResult();
        return result.put(housingService.getHousingMiMap());
    }

    /**
     * 功能描述:
     * 获取所有房源资料(字段可扩展)
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/3 11:01
     */
    @GetMapping("/getAllComIdHousingList")
    public JsonResult getAllComIdHousingList(@RequestParam Map<String,Object>params){
        JsonResult result=new JsonResult();
        return result.put(housingService.getAllComIdHousingList(params));
    }

    /**
     * 功能描述:
     * 根据盈利预测室号,获取所有的房源出租房间列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/20 18:57
     */
    @PostMapping("/getProByHousingInfoList")
    public JsonResult getProByHousingInfoList(@RequestBody Map<String,Object>params){
        if(FieldCheckUtil.isMapFieldNotNull(params,new String[]{"houIdList"})){
            List<String>comList=(List<String>)params.get("houIdList");
            if(comList.size()==0){
                return JsonResult.error("houIdList不能为空！");
            }
        }
        return JsonResult.success(housingService.getProByHousingInfoList(params));
    }

    /**
     * 通过项目编号, 楼号, 室号, 查到所有的房源id数组
     **/
    @GetMapping("/getHouseIdListByBuildAndRoom")
    public List<String> getHouseIdListByBuildAndRoom(@RequestParam Map<String, Object> params) {
        return housingMapper.getHouseIdListByBuildAndRoom(params);
    }
}
